package nl.nickthissen.remindme.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import nl.nickthissen.remindme.R;
import nl.nickthissen.remindme.activities.MainActivity;
import nl.nickthissen.remindme.common.Utils;
import nl.nickthissen.remindme.data.ReminderItem;
import nl.nickthissen.remindme.views.ColorButton;

/**
 * Created by nthissen on 11/06/13.
 */
public class ReminderItemAdapter extends ArrayAdapter<ReminderItem>
{
    private MainActivity activity;
    private ArrayList<ReminderItem> items;

    public ReminderItemAdapter(MainActivity context, int resourceId, ArrayList<ReminderItem> items)
    {
        super(context, resourceId, items);
        this.activity = context;
        this.items = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View v = convertView;
        if (v == null)
        {
            LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.item_row, null);
        }

        ReminderItem item = items.get(position);
        if (item != null)
        {
            //v.setClickable(true);
            //v.setOnClickListener(new ItemClickListener(item));

            LinearLayout colorLayout = (LinearLayout) v.findViewById(R.id.colorLayout);
            TextView lblTitle = (TextView) v.findViewById(R.id.lblTitle);
            TextView lblCategory = (TextView) v.findViewById(R.id.lblCategory);
            TextView lblDate = (TextView) v.findViewById(R.id.lblDate);

            colorLayout.setBackgroundColor(item.category.color);
            lblCategory.setText(item.category.name);
            lblTitle.setText(item.title);
            lblDate.setText(Utils.dateToString(item.reminderDate));
        }

        return v;
    }

    private class ItemClickListener implements View.OnClickListener
    {
        private ReminderItem item;

        public ItemClickListener(ReminderItem i)
        {
            this.item = i;
        }

        @Override
        public void onClick(View v)
        {
            activity.onItemClicked(item);
        }
    }
}
